package com.zhangzhan.controller;

import com.zhangzhan.domain.commcon.ResponseResult;
import com.zhangzhan.domain.entity.User;
import com.zhangzhan.domain.enums.AppHttpCodeEnum;
import com.zhangzhan.domain.vo.AdminUserInfoVo;
import com.zhangzhan.domain.vo.MenuVO;
import com.zhangzhan.domain.vo.RoutersVo;
import com.zhangzhan.exception.MySystemException;
import com.zhangzhan.service.LoginService;
import com.zhangzhan.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "后台登录接口")
public class LoginController {

    @Autowired
    private LoginService loginService;



    /**
     * @description 用户登录接口
     * @param user
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/5 10:31
     */
    @PostMapping("/user/login")
    @ApiOperation(value = "/user/login")
    public ResponseResult login(@RequestBody User user){
        if(!StringUtils.hasText(user.getUserName())){
            //提示 必须要传用户名
            throw new MySystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return loginService.login(user);
    }

    /**
     * @description 退出登录接口
     *
     * @return com.zhangzhan.domain.commcon.ResponseResult
     * @author 31115
     * @date 2022/12/6 14:16
     */
    @PostMapping("/user/logout")
    public ResponseResult logout(){
        return loginService.logout();
    }
    /**
     * @description 用户权限查询
     *
     * @return com.zhangzhan.domain.commcon.ResponseResult<com.zhangzhan.domain.vo.AdminUserInfoVo>
     * @author 31115
     * @date 2022/12/5 14:59
     */
    @GetMapping("/getInfo")
    @ApiOperation(value = "/getInfo",tags = "用户权限、角色查询")
    public ResponseResult<AdminUserInfoVo> getInfo(){
        return loginService.getInfo();
    }

    /**
     * @description 动态路由
     *
     * @return com.zhangzhan.domain.commcon.ResponseResult<com.zhangzhan.domain.vo.MenuVO>
     * @author 31115
     * @date 2022/12/6 11:07
     */
    @GetMapping("/getRouters")
    @ApiOperation(value = "/getRouters",tags = "页面动态路由展示")
    public ResponseResult<MenuVO> getRouters(){
        //获取当前用户id
        Long userId = SecurityUtils.getUserId();
        //调用service层
       List<MenuVO> menus = loginService.getRouters(userId);

       return ResponseResult.okResult(new RoutersVo(menus));
    }
}